package com.nowcoder.community.controller;

import com.nowcoder.community.pojo.DiscussPost;
import com.nowcoder.community.pojo.Page;
import com.nowcoder.community.pojo.User;
import com.nowcoder.community.service.ElasticsearchService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "搜索数据相关")
@Controller
public class SearchController implements CommunityConstant {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    @Qualifier("likeService")
    private LikeService likeService;

    @Autowired
    private ElasticsearchService elasticsearchService;

    //搜索数据
    @ApiOperation(value = "搜索数据")
    @GetMapping("/search")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词"),
            @ApiImplicitParam(name = "page", value = "分页实例")
    })
    public String search(String keyword, Page page, Model model){
        //搜索帖子
        try {
            Object[] objects = elasticsearchService.searchDiscussPost(
                    keyword, page.getCurrent() - 1, page.getLimit());

            //获取Elasticsearch中的帖子列表
            List<DiscussPost> searchResult = (List<DiscussPost>)objects[0];

            //获取总命中帖子数
            long totalDiscussPosts = (long) objects[1];

            //聚合数据
            List<Map<String, Object>> discussPosts = new ArrayList<>();

            if (searchResult != null){
                for (DiscussPost post : searchResult) {
                    Map<String, Object> map = new HashMap<>();
                    //帖子
                    map.put("post", post);

                    //--------------------------------------------------------
                    User user = userService.findUserById(post.getUserId());
//                    System.out.println(user);
                    //作者
                    map.put("author", user);


                    //点赞数量
                    map.put("likeCount", likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, post.getId()));

                    discussPosts.add(map);
                }

                model.addAttribute("discussPosts", discussPosts);
                model.addAttribute("keyword", keyword);

                //分页信息
                page.setPath("/search?keyword=" + keyword);

                //获取总页数
                page.setRows(searchResult == null ? 0 : (int)totalDiscussPosts);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "site/search";
    }

}
